package com.ruoyi.system.service.admin;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.CommonConstant;
import com.ruoyi.common.core.domain.entity.admin.AdminCustomerDB;
import com.ruoyi.common.core.domain.entity.customer.AdminIntentionDB;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserTypeEnum;
import com.ruoyi.common.utils.sql.MyPageUtils;
import com.ruoyi.system.mapper.admin.AdminCustomerMapper;
import com.ruoyi.system.mapper.admin.AdminIntentionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

@Service
@Slf4j
public class AdminIntentionService {
    @Resource
    private AdminIntentionMapper adminIntentionMapper;
    @Resource
    private AdminCustomerMapper adminCustomerMapper;


    public TableDataInfo selectCustomerIntentionDBList(AdminIntentionDB adminIntentionDB) {
        LambdaQueryWrapper<AdminIntentionDB> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtils.isNotEmpty(adminIntentionDB.getUserName()),AdminIntentionDB::getUserName, adminIntentionDB.getUserName());
        //intentionAddressNo
        wrapper.eq(ObjectUtils.isNotEmpty(adminIntentionDB.getIntentionAddressNo()),AdminIntentionDB::getIntentionAddressNo, adminIntentionDB.getIntentionAddressNo());
        // userPhoneNumber
        wrapper.like(ObjectUtils.isNotEmpty(adminIntentionDB.getUserPhoneNumber()),AdminIntentionDB::getUserPhoneNumber, adminIntentionDB.getUserPhoneNumber());
        // 是否启用
        wrapper.eq(ObjectUtils.isNotEmpty(adminIntentionDB.getIsActive()),AdminIntentionDB::getIsActive, adminIntentionDB.getIsActive());
        // 按照手机号倒叙，手机号一样按照id倒叙
//        wrapper.orderByAsc(AdminIntentionDB::getUserPhoneNumber);
        wrapper.orderByDesc(AdminIntentionDB::getId);

        // 分页
        Page<AdminIntentionDB> pageParam = MyPageUtils.handlePageParam();
        Page<AdminIntentionDB> adminIntentionDBPage = adminIntentionMapper.selectPage(pageParam, wrapper);
        return TableDataInfo.covert(adminIntentionDBPage);
    }

    public AdminIntentionDB selectCustomerIntentionDBById(Long id) {
       return adminIntentionMapper.selectById(id);
    }

    public int insertCustomerIntentionDB(AdminIntentionDB adminIntentionDB) {
        return adminIntentionMapper.insert(adminIntentionDB);
    }

    public int updateCustomerIntentionDB(AdminIntentionDB adminIntentionDB) {
        return adminIntentionMapper.updateById(adminIntentionDB);
    }

    public int deleteCustomerIntentionDBByIds(Long[] ids) {
        // 更改删除时间
        LambdaUpdateWrapper<AdminIntentionDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(AdminIntentionDB::getId, Arrays.asList(ids));
        updateWrapper.set(AdminIntentionDB::getDeletedAt, DateUtil.date());
        adminIntentionMapper.update(updateWrapper);

        // 使用了 @TableLogic 注解后,将这批id的删除状态置为1
        return adminIntentionMapper.deleteBatchIds(Arrays.asList(ids));
    }

    public int updateIsNotified(Long id) {
        LambdaUpdateWrapper<AdminIntentionDB> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AdminIntentionDB::getId, id);
        wrapper.set(AdminIntentionDB::getIsNotified, CommonConstant.CONSTANT_NUM.ONE);
        return adminIntentionMapper.update(wrapper);
    }

    public int updateIsHave(Long id) {
        // 查询该用户的id
        LambdaUpdateWrapper<AdminCustomerDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AdminCustomerDB::getUserType, UserTypeEnum.MEMBER.getCode());
        updateWrapper.eq(AdminCustomerDB::getId, id);
        adminCustomerMapper.update(updateWrapper);

        LambdaUpdateWrapper<AdminIntentionDB> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AdminIntentionDB::getId, id);
        wrapper.set(AdminIntentionDB::getIsHave, CommonConstant.CONSTANT_NUM.ONE);

        return adminIntentionMapper.update(wrapper);
    }


}
